/*
 * @Author: szx
 * @Date: 2022-03-13 13:54:17
 * @LastEditTime: 2022-03-13 14:04:56
 * @Description:
 * @FilePath: \leetcode\1-99\46\46_2.js
 */
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permute = function (nums) {
    const n = nums.length;
    const res = [];
    const used = new Array(n).fill(false);
    const backtrace = (path, index) => {
        if (path.length == n) {
            res.push(Array.from(path));
            return;
        }
        for (let i = 0; i < n; i++) {
            if (used[i]) continue;
            used[i] = true;
            path.push(nums[i]);
            backtrace(path, i + 1);
            path.pop();
            used[i] = false;
        }
    };
    backtrace([], 0);
    return res;
};
console.log(permute([1, 2, 3]));
